<?php

 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.model' );
 

class restoranModelrestoran extends JModel
{
  function getDatasCurl(){
        $config_params =& JComponentHelper::getParams( 'com_restoran' );
        $url_server = $config_params->get('url_server');
        $apikey = $config_params->get('api_key');        
		$url_server = $url_server."food/idapi=".$apikey;

        if (empty ($apikey)){
            echo JText::_( 'Anda Belum Menginputkan Api Key Pada Parameter' );
        }else{
            $ch = curl_init();
            //curl_setopt($ch, CURLOPT_URL,$url_server);
            curl_setopt($ch, CURLOPT_URL,$url_server);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $data = curl_exec($ch);
            curl_close($ch);
            $xml2array = $this->fetch_xml($data);

          return $xml2array;
        }
        
  }

    function fetch_xml($xml){

    if(is_file($xml)){
        $xml_data = file_get_contents($xml);
    }
    else{
        $xml_data = $xml;
    }
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr);
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    $root_tag = $assoc_arr[0]['tag'];
    $base_tag = strtolower($assoc_arr[1]['tag']);
    $i = 0;
    foreach($assoc_arr as $key => $element){
        if($element['tag'] != $root_tag){
            if(!preg_match('/^\s+$/', $element['value'])){
                $tag = strtolower($element['tag']);
                $items[$i][$tag] = $element['value'];
                if($tag == $base_tag){
                    $i++;
                }
            }
            elseif(isset($element['attributes'])){
                $items[$i]['id'] = $element['attributes']['ID'];
            }
        }
    }

    return $items;
    } //end of function fetchxml
	
}
